// pages/shopping_cart/shopping_cart.js

Page({
  data: {
    fullChecked: false,
    showOps: false,
    books: [{
      id: 1,
      ISBN:'9787040396638',
      bookName: "高等数学",
      version: "第七版上册",
      publish: "高等教育出版社",
      amount: '2.00',                 // 单价
      num: 2,                    // 库存
      bookCheck: false
    },{
      id: 2,
      ISBN:'9787115379504',
      bookName: "数据结构",
      version: "第二版",
      publish: "人民邮电出版社",
      amount: '2.00',
      inventory: 2,                    // 库存
      bookCheck: false
    },{
      id: 3,
      ISBN:'9787302360520',
      bookName: "Java程序设计",
      version: "第二版",
      publish: "清华大学出版社",
      amount: '2.00',
      inventory: 2,                    // 库存
      bookCheck: false
    }],
    result: [],
    imageURL: 'https://img12.360buyimg.com/babel/s380x300_jfs/t1/152314/13/19839/57522/603e118dE941f0ce9/fdff58457adbef3e.jpg.webp'
  },
  // 全选按钮控制方法
  changeFullChecked() {
    const check = !this.data.fullChecked

    // 根据fullCheched修改books的bookCheck
    if (this.data.fullChecked === true) {
      // 全选 --> 取消全选
      let books = this.data.books.map(book => {
        book.bookCheck = false
        return book
      })
      this.setData({
        books
      })
    } else {
      // 取消全选 --> 全选
      let books = this.data.books.map(book => {
        book.bookCheck = true
        return book
      })
      this.setData({
        books
      })
    }
    
    this.setData({
      fullChecked: check
    })
  },
  // 修改订单长按弹出框
  changeShowOps() {
    this.setData({
      showOps: !this.data.showOps
    })
  },
  // 选择/取消订单，进行结算
  selectGood(e) {
    const bookId = e.currentTarget.dataset.id
    // 查找id相等的图书index
    let books = this.data.books
    let index = books.findIndex(book => {
      return book.id === bookId
    })
    const tempBook = books[index]
    tempBook.bookCheck = !tempBook.bookCheck
    // tempBook替换指定index的项
    books.splice(index, 1, tempBook)
    this.setData({
      books
    })
    // 返回books中未选中的book
    let bookArr = this.data.books.filter(book => {
      return book.bookCheck === false
    })
    if (bookArr.length === 0) {
      // 未选中的数组长度为0
      this.setData({
        fullChecked: true
      })
    } else {
      // 未选中的数组长度非0
      this.setData({
        fullChecked: false
      })
    }
  },
  // 删除购物车中商品
  deleteGood() {
    let that = this
    wx.showModal({
      title: '删除订单',
      content: '是否确认删除？',
      success (res) {
        if (res.confirm) {
          // 调用后台API，删除购物车内订单
          console.log('调用后台API，删除购物车内订单');
          that.changeShowOps()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 提交购物车的订单
  submitShelves() {
    console.log('调用后台API，提交订单');
    let that = this
    wx.showModal({
      title: '下单商品',
      content: '是否购买当前所选商品？',
      success (res) {
        if (res.confirm) {
          // 调用后台API，删除购物车内订单
          console.log('调用后台API，购买当前所选商品，选中的订单状态改为挂起');
          let tempShopping = that.data.books.filter(book => {
            return book.bookCheck === true
          })
          // 调用后台API
          that.asyncSubmitShelves(tempShopping)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 调用后台API，获取我的购物车
  getMyShoppingCart() {
    // 调用后台shopping_cart API
    const ui = wx.getStorageSync('userinfo')
    wx.cloud.callFunction({
      name: 'shopping_cart',
      data: {
        openid: ui.openid
      },
      success: res => {
        console.log('shopping_cart的回调：', res);
      },
      fail: res => {
        console.log('shopping_cart调用失败，错误信息：', res);
      }
    })
  },
  // 异步请求后台提交订单API
  asyncSubmitShelves(list) {
    const ui = wx.getStorageSync('userinfo')
     let shoppingList = list.map(item => {
      let obj = {
        id: item.id,
        num: item.num,
        openid: ui.openid
      }
      return obj
    })
    console.log('shoppingList', shoppingList);
    wx.cloud.callFunction({
      name: 'do_shopping',
      data: {
        shoppingList
      },
      success: res => {
        console.log('do_shopping的回调：', res);
      },
      fail: res => {
        console.log('do_shopping调用失败，错误信息：', res);
      }
    })
  },
  onLoad() {
    // 调用后台API，获取我的购物车
    this.getMyShoppingCart()
  }
})